import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.codec.StringCodec;

public class RedissonStringCodecConfig {

    public static RedissonClient createClient() {
        Config config = new Config();
        config.useClusterServers()
                .addNodeAddress("redis://192.168.1.10:7001",
                        "redis://192.168.1.10:7002",
                        "redis://192.168.1.10:7003",
                        "redis://192.168.1.10:7004",
                        "redis://192.168.1.10:7005",
                        "redis://192.168.1.10:7006")
                .setConnectTimeout(10000)
                .setTimeout(3000)
                .setRetryAttempts(3);

        // 设置 StringCodec new JsonJacksonCodec()
        config.setCodec(new StringCodec());

        return Redisson.create(config);
    }
}



import com.google.protobuf.Message;
import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.Decoder;
import org.redisson.client.protocol.Encoder;
import org.redisson.codec.BaseCodec;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;

import java.io.IOException;

public class ProtobufCodec<T extends Message> extends BaseCodec {

    private final Class<T> messageClass;
    private final Message prototype;

    public ProtobufCodec(Class<T> messageClass, Message prototype) {
        this.messageClass = messageClass;
        this.prototype = prototype;
    }

    // 编码器：Java 对象 → 字节数组
    @Override
    public Encoder getEncoder() {
        return (Object obj) -> {
            if (!(obj instanceof Message)) {
                throw new IllegalArgumentException("Object must be a Protobuf Message");
            }
            Message message = (Message) obj;
            return message.toByteArray();
        };
    }

    // 解码器：字节数组 → Java 对象
    @Override
    public Decoder getDecoder() {
        return (ByteBuf buf) -> {
            byte[] bytes = new byte[buf.readableBytes()];
            buf.readBytes(bytes);
            return prototype.getParserForType().parseFrom(bytes);
        };
    }
}

